package com.fh.controller.app.appuser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fh.apientity.Symbol;
import com.fh.controller.base.BaseController;
import com.fh.service.system.redis.RedisService;
import com.fh.service.system.user.AssetsUserService;
import com.fh.service.transactionpair.TransactionPairService;
import com.fh.util.ApiClientUtil;
import com.fh.util.AppUtil;
import com.fh.util.Base64;
import com.fh.util.ClassNameUtil;
import com.fh.util.StatusCode;

import com.fh.util.EncryptUtil;
import com.fh.util.HttpConfig;
import com.fh.util.JsonStringUtil;
import com.fh.util.PageData;

import net.sf.json.JSONObject;
/**
 * 
 * 
 * 
 * 首页接口
*/
@Controller
@RequestMapping(value="/exc/home")
public class HomeMarketController extends BaseController  {
	
	@Resource(name="transactionpairService")
	private TransactionPairService transactionpairService;
	@Resource(name="redisService")
	private RedisService redisService;
	@Resource(name="assetsuserService")
	private AssetsUserService assetsuserService;
	
	
	/**
	 * 获取资产账号，比如 普通账号，法币账号，总币
	 */
	@RequestMapping(value=ClassNameUtil.getAccountAsset,produces =HttpConfig.CONTENT_TYPE_UTF8, method=RequestMethod.POST)
	@ResponseBody
	public Object getAccountAsset(HttpSession session,HttpServletRequest request) {
		 logBefore(logger, "开始调首页资产账号接口");
		 Object result ="";
		 PageData pd = new PageData();
		 Map<String,Object> map = new  HashMap<>();
		 try{
			 Object parameter = pd.getParameters(request);
			 JSONObject jObject = JSONObject.fromObject(parameter);
			 logBefore(logger, "调首页资产账号接口用户id:"+jObject.getString("userId"));
			 String userId = new String(EncryptUtil.Base64decode(jObject.getString("userId")));
			 pd.put("USER_ID", userId);
			 if(redisService.exists(userId)){
				 List<PageData> pageDataList = assetsuserService.sumlistAll(pd);
				 map.put("pageDataList", pageDataList);
				 result=  EncryptUtil.AESEncode(JsonStringUtil.getJSONResult(StatusCode.SUCCESS,"成功",map).toString());
			 }
			
		 }catch(Exception e){
			 logger.error(e.toString(),e);
			 result=JsonStringUtil.getJSONResult(StatusCode.ERROR_INTERFACE,"接口校验失败","");
			 
		 }finally{
	    	 return result;
		}
	}
		
	
	
	/**
	 * 获取资产账号接口列表
	 */
	@RequestMapping(value=ClassNameUtil.getAccountAssetList,produces =HttpConfig.CONTENT_TYPE_UTF8, method=RequestMethod.POST)
	@ResponseBody
	public Object getAccountAssetList(HttpSession session,HttpServletRequest request) {
		 logBefore(logger, "开始调账号接口");
		 PageData pd = new PageData();
		 Object result ="";
		 Map<String,Object> map = new  HashMap<>();
		 try{
			 Object parameter = pd.getParameters(request);
			 JSONObject jObject = JSONObject.fromObject(parameter);
			 logBefore(logger, "开始调账号接口用户id:"+jObject.getString("userId"));
			 String userId = new String(EncryptUtil.Base64decode(jObject.getString("userId")));
			 String ACCOUNT_TYPE = new String(EncryptUtil.Base64decode(jObject.getString("ACCOUNT_TYPE")));
			 pd.put("USER_ID", userId);
			 pd.put("ACCOUNT_TYPE", ACCOUNT_TYPE);
			 if(redisService.exists(userId)){
				 List<PageData> pageDataList = assetsuserService.listAll(pd);
				 map.put("data", pageDataList);
				 result= EncryptUtil.AESEncode(JsonStringUtil.getJSONResult(StatusCode.SUCCESS,"成功",map).toString());
			 }
		 }catch(Exception e){
			 logger.error(e.toString(),e);
			 result=JsonStringUtil.getJSONResult(StatusCode.ERROR_INTERFACE,"接口校验失败","");
			 
		 }finally{
	    	 return result;
		}

		
	}
	
}
